package mbp.agents;

import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;

import java.util.ArrayList;
import java.util.Iterator;

import mbp.agents.MbpStorageAgent.ResourceChecker;
import mbp.core.Resource;
import mbp.gui.frames.ResourceStorageGui;
import mbp.message.Message;

public class TestAgent extends AMbpAgentMap
{
	private ResourceStorageGui agentGui;
	
	public void setup()
	{
		super.setup();
		
		//agentGui = new ResourceStorageGui(this);
		//agentGui.setVisible(true);
		ResourceChecker resourceChecker = new ResourceChecker(this, 1000);
		addBehaviour(resourceChecker);
		
		System.out.println("This is my setup");
	}
	
	/**
	 * Sprawdzenie zasobu pod katem waznosci. Zasoby ktorych termin waznosci uplynal sa usuwane.
	 * @author Wojciech Wilanski
	 *
	 */
	public class ResourceChecker extends TickerBehaviour
	{

		public ResourceChecker(Agent a, long period) {
			super(a, period);
		}

		@Override
		protected void onTick() 
		{
			Iterator<String> it = resourceList.keySet().iterator();
			ArrayList<String> toRemove = new ArrayList<String>();
			//Boolean removed = false;
			
			//Wyszukanie zasobow przeterminowanych
			while(it.hasNext())
			{
				String keyUuid = it.next();
				
				Resource rc = resourceList.get(keyUuid);
				
				if(!rc.isValid())
				{
					toRemove.add(keyUuid);
					//removed = true;
				}
			}
			
			//usuniecie zasobow przeterminowanych
			for(int i = 0; i<toRemove.size(); i++)
			{
				resourceList.remove(toRemove.get(i));
			}
			
			//Odswierzenie listy na gui
			//if(removed)
			//{
			//	if(agentGui != null)
			//	{
			//		agentGui.ReloadResources(resourceList);
			//	}
			//}
		}
	}

	@Override
	public void processMessage(Message iMsg, String senderName) {
		// TODO Auto-generated method stub
		
	}

}
